package com.shopping.huaju.commodity.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.shopping.huaju.commodity.entity.AttrAttrgroupRelationEntity;
import com.shopping.huaju.commodity.entity.AttrEntity;
import com.shopping.huaju.commodity.entity.vo.AttrGroupWithAttrsVo;
//import com.shopping.huaju.commodity.feign.CouponFeignService;
import com.shopping.huaju.commodity.feign.CouponFeignService;
import com.shopping.huaju.commodity.service.AttrService;
import com.shopping.huaju.commodity.service.CategoryService;
import com.shopping.huaju.common.to.SpuBoundTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.shopping.huaju.commodity.entity.AttrgroupEntity;
import com.shopping.huaju.commodity.service.AttrgroupService;
import com.shopping.huaju.common.utils.PageUtils;
import com.shopping.huaju.common.utils.R;

import javax.annotation.Resource;


/**
 * 家居商品属性分组
 *
 * @author tofu
 * @email tofu@qq.com
 * @date 2023-11-06 21:31:44
 */
@RestController
@RequestMapping("commodity/attrgroup")
public class AttrgroupController {
    @Autowired
    private AttrgroupService attrgroupService;
    @Resource
    private CategoryService categoryService;

    @Resource
    private AttrService attrService;
    //@Resource
    //private CouponFeignService couponFeignService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("commodity:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrgroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 自定义列表 -- 实现根据categoryId 和 id 、 name 三个条件查询返回分页数据结果
     * @param params
     * @return
     */
    @RequestMapping("/list/{categoryId}")
    //@RequiresPermissions("commodity:attrgroup:list")
    public R listWithCondition(@RequestParam Map<String, Object> params, @PathVariable("categoryId") Long categoryId){
        PageUtils page = attrgroupService.queryPage(params,categoryId);
        return R.ok().put("page", page);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("commodity:attrgroup:info")
    public R info(@PathVariable("id") Long id){
		AttrgroupEntity attrgroup = attrgroupService.getById(id);
        //为返回的 AttrgroupEntity 对象添加 该对象对应的分类对象的 层级id信息
        attrgroup.setCascaderCategoryId(categoryService.getCascaderCategoryId(attrgroup.getCategoryId()));
        return R.ok().put("attrgroup", attrgroup);
    }
    /**
     * 根据 分类 获得分类分层id结果
     */
    @RequestMapping("/info/cascader/{categoryId}")
    public R cascader(@PathVariable("categoryId") Long categoryId){
        Long[] cascaderCategoryId = categoryService.getCascaderCategoryId(categoryId);
        return R.ok().put("cascaderCategoryId",cascaderCategoryId);
    }

    /**
     * 根据 属性组id  获取该属性组对应的所有基本属性的对象信息
     */
    @RequestMapping("/{attrGroupId}/attr/relation")
    public R getRelationAttrs(@PathVariable("attrGroupId") Long attrGroupId){
        List<AttrEntity> attrEntities = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data",attrEntities);
    }

    /**
     * 根据属性组id 获取该属性组 对应的可以被添加的基本属性的对象信息
     */
    @RequestMapping("/{attrGroupId}/attr/allowrelation")
    public R getAllowedAttrs(@PathVariable Long attrGroupId,@RequestParam Map<String, Object> params){
        PageUtils page = attrService.getAllowedAttrs(params, attrGroupId);
        return R.ok().put("page",page);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("commodity:attrgroup:save")
    public R save(@RequestBody AttrgroupEntity attrgroup){
		attrgroupService.save(attrgroup);

        return R.ok();
    }
    /**
     * 根据业务需求, 根据属性关联对象的属性id和属性组id 将属性关联对象  保存/批量 到数据库中
     */
    @RequestMapping("/attr/relation")
    public R saveRelation(@RequestBody AttrAttrgroupRelationEntity[] attrAttrgroupRelationEntities){
        attrgroupService.saveRelation(attrAttrgroupRelationEntities);
        return R.ok();
    }
    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("commodity:attrgroup:update")
    public R update(@RequestBody AttrgroupEntity attrgroup){
		attrgroupService.updateById(attrgroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("commodity:attrgroup:delete")
    public R delete(@RequestBody Long[] ids){
		attrgroupService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    /**
     * 根据业务需求, 根据属性关联对象的属性id和属性组id 删除 关联关系
     */
    @RequestMapping("/attr/relation/delete")
    public R relationRemove(@RequestBody AttrAttrgroupRelationEntity[] attrAttrgroupRelationEntities){
        attrgroupService.deleteRelation(attrAttrgroupRelationEntities);
        return R.ok();
    }
    /**
     * 根据业务需求，根据分类id 返回分类下的属性组和基本属性数据 要求按层级结构
     */

    @RequestMapping("/{categoryId}/withattr")
    public R getAttrGroupWithAttrsVo(@PathVariable Long categoryId){
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVo = attrgroupService.getAttrGroupWithAttrsVo(categoryId);
        return R.ok().put("data",attrGroupWithAttrsVo);
    }

    //@RequestMapping("/test")
    //public R test() {
    //    SpuBoundTo spuBoundTo = new SpuBoundTo();
    //    System.out.println("===============");
    //    spuBoundTo.setSpuId(5l);
    //    spuBoundTo.setBuyBounds(new BigDecimal(30));
    //    spuBoundTo.setBuyBounds(new BigDecimal(30));
    //    R r = couponFeignService.saveSpusBound(spuBoundTo);
    //    return r;
    //}
}
